package dao

import javax.inject.Inject

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global


/**
  * Created by yz on 2017/6/1.
  */
class CirRNAProfileDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insertAll(circrnaprofiles: Seq[CircrnaprofileRow]): Future[Unit] = db.run(Circrnaprofile ++=
    circrnaprofiles).map(_ => ())

  def selectAll(): Future[Seq[CircrnaprofileRow]] = db.run(Circrnaprofile.result)

  def deleteAll(): Future[Unit] = db.run(Circrnaprofile.delete).map(_ => ())

  def selectByCircIds(circIds: Seq[String]): Future[Seq[CircrnaprofileRow]] = {
    db.run(Circrnaprofile.filter(_.circid.inSetBind(circIds)).result)
  }

  def dropTable(): Future[Unit] = {
    val sqlu = DBIO.seq(
      sqlu"""
          DROP TABLE IF EXISTS circrnaprofile;
        """)
    db.run(sqlu).map(_ => ())
  }

  def createTable(): Future[Unit] = {
    val sqlu = DBIO.seq(
      sqlu"""
        CREATE TABLE `circrnaprofile` (
              `circId` varchar(255) NOT NULL,
              `sampleName` varchar(255) NOT NULL,
              `value` double NOT NULL,
             `type` varchar(255) NOT NULL,
              PRIMARY KEY (`circId`,`sampleName`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        """)
    db.run(sqlu).map(_ => ())
  }


}